//
//  KYTabBarViewController.m
//  fangKuaiYi
//
//  Created by Liu on 2017/1/7.
//  Copyright © 2017年 seconTeam. All rights reserved.
//

#import "KYTabBarViewController.h"
#import "CZAdditions.h"


@interface KYTabBarViewController ()

@property(nonatomic, strong) UIButton *openDrawerButton;


@end

@implementation KYTabBarViewController

#pragma mark - Managing the view

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    //初始化三横键
    self.openDrawerButton = [UIButton buttonWithType:UIButtonTypeCustom];
    //三横键的位置
    self.openDrawerButton.frame = CGRectMake(10.0f, 20.0f, 44.0f, 44.0f);
    //三横键的图标
    
    [self.openDrawerButton setTitle:@"侧滑" forState:UIControlStateNormal];
    
    [self.openDrawerButton setBackgroundColor:[UIColor redColor]];
//    [self.openDrawerButton setImage:hamburger forState:UIControlStateNormal];
    //点击三横键的方法
    [self.openDrawerButton addTarget:self action:@selector(openDrawer:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:self.openDrawerButton];
    
    //添加子控制器
    [self addChildViewControllers];
    
    //设置tabbar的tintColor
    self.tabBar.tintColor = [UIColor cz_colorWithHex:0xdf0000];
}




//添加所有子控制器
- (void) addChildViewControllers {
    
    //所有子控制器的参数
    NSArray *parameters = @[
                            @{@"clsName": @"KYHomeController", @"title": @"首页", @"icon": @"news"},
                            @{@"clsName": @"KYFoundController", @"title": @"发现", @"icon": @"reader"},
                            @{@"clsName": @"KYHealthController", @"title": @"健康", @"icon": @"media"}
                            
                            ];
    
    //子控制器数组
    NSMutableArray *array = [NSMutableArray array];
    
    for (NSDictionary *dic in parameters) {
        UIViewController *controller = [self childViewControllerWithDic:dic];
        [array addObject:controller];
    }
    
    self.viewControllers = array;
}


//创建一个子控制器
- (UIViewController *) childViewControllerWithDic:(NSDictionary *)dic {
    //用类名来创建一个控制器
    //好处：不需要导入头文件
    NSString *clsName = dic[@"clsName"];
    Class cls = NSClassFromString(clsName);
    UIViewController *controler = [cls new];
    
    //设title
    controler.title = dic[@"title"];
    
    //设tabbarItem的图标
    NSString *normalImageName = [NSString stringWithFormat:@"tabbar_icon_%@_normal", dic[@"icon"]];
    NSString *hlImageName = [NSString stringWithFormat:@"tabbar_icon_%@_highlight", dic[@"icon"]];
    
    [controler.tabBarItem setImage:[UIImage imageNamed:normalImageName]];
    [controler.tabBarItem setSelectedImage:[[UIImage imageNamed:hlImageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:controler];
    
        
    return nav;
}

#pragma mark - Configuring the view’s layout behavior

//隐藏状态栏
- (BOOL)prefersStatusBarHidden
{
    return NO;
}
//状态栏的样式
- (UIStatusBarStyle)preferredStatusBarStyle
{
    //文字白色
    return UIStatusBarStyleLightContent;
    
    //    //文字黑色
    //    return UIStatusBarStyleDefault;
    
}

#pragma mark - ICSDrawerControllerPresenting


//当侧边栏打开 关闭当前的控制器用户交互
- (void)drawerControllerWillOpen:(ICSDrawerController *)drawerController
{
    self.view.userInteractionEnabled = NO;
}

//当侧边栏关闭 开启当前的控制器用户交互
- (void)drawerControllerDidClose:(ICSDrawerController *)drawerController
{
    self.view.userInteractionEnabled = YES;
}

#pragma mark - Open drawer button

//当点击三横键 打开侧边栏
- (void)openDrawer:(id)sender
{
    [self.drawer open];
}



@end
